//
//  HelpDetailViewController.m
//  SafeFoodManagerDemo
//
//  Created by ios-1 on 2018/3/13.
//  Copyright © 2018年 bob. All rights reserved.
//

#import "HelpDetailViewController.h"
#import "LookInforTableViewCell.h"
#import "RSTableHeadView.h"
//#import "LookInforModel.h"
#import "HelpDetailModel.h"
#import "RegisterMorePicTableViewCell.h"
#import "RegisterSpicTableViewCell.h"
@interface HelpDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)LookInforModel *inforModel;

@property(nonatomic,copy)NSString*headimage;
@end

@implementation HelpDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isScanphotos=YES;
    
    self.headArray = @[@"品名",@"分类",@"描述",@"图片"];
    
    [self.view addSubview:self.homeTableView];
    
    [self.view addSubview:self.footView];
    
    [self loadDatas];
    
}

-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _homeTableView.estimatedRowHeight=44;
        
        _homeTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"LookInforTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        [_homeTableView registerClass:[RegisterMorePicTableViewCell class] forCellReuseIdentifier:@"TypeCell3"];
        
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"RegisterSpicTableViewCell" bundle:nil] forCellReuseIdentifier:@"TypeCell5"];
        
        [_homeTableView registerClass:[RSTableHeadView class] forHeaderFooterViewReuseIdentifier:@"head"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    LookInforList *list=self.dataArray[indexPath.section];
    
    if (indexPath.section < 3) {
        
        
        LookInforTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.InforLabel.text = list.name;
        
        return cell;
        
    }else
    {
        
        RegisterMorePicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell3" forIndexPath:indexPath];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        cell.isEdit=NO;//是否可编辑
        
        cell.ImageArr=[HelpWay imageStrToArr:list.name];//传递图片数组
        
        [cell updateLebeframe];
        
        TBWeak;
        //浏览图片
        cell.ScanBlock=^(NSInteger num)
        {
            [weakSelf.ScanPhotoArr removeAllObjects];
            
            for (NSString*imagestr in [HelpWay imageStrToArr:list.name]) {
                
                [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
            }
            
            [weakSelf GoToPhotoScanAndIndex:num];
        };
        
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    LookInforList *list=self.dataArray[indexPath.section];
    
    if (indexPath.section < 3) {
        
        return 60;
        
    }else
    {
        NSArray *arr=[HelpWay imageStrToArr:list.name];
        
        if (arr.count==0) {
            
            return 43;
            
        }else{
            
            NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
            
            CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
            
            return 15+10*(colorm-1)+colorm*cellHeight+47;
            
        }
    }
    
    return UITableViewAutomaticDimension;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *title = self.headArray[section];
    RSTableHeadView *head=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    
    head.Rtitle=title;
    return head;
    
}

-(void)ShowHEAD
{
    [self.ScanPhotoArr removeAllObjects];
    
    [self.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,self.headimage]]]];
    
    [self GoToPhotoScanAndIndex:0];
    
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:self.reqUrl andrequestTime:5 andParams:self.params andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        HelpDetailModel *detailModel = [HelpDetailModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
            NSLog(@"%@",[HelpWay dictionaryToJSON:responseObject]);
            if (!detailModel.data) {
                
                self.Type=TBNODateType;
                
            }else{
                
                for(int i =0; i< self.headArray.count; i++)
                {
                    LookInforList *item = [LookInforList new];
                    if(i == 0)
                    {
                        item.name = detailModel.data.name;
                    }else if (i==1)
                    {
                        item.name = detailModel.data.catName;
                    }else if (i==2)
                    {
                        item.name = detailModel.data.intro;
                    }else if (i==3)
                    {
                        item.name = detailModel.data.imgs;
                    }
                    [self.dataArray addObject:item];
                }
                
                [self.homeTableView reloadData];
                
            }
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
    
}

- (UIView*)footView
{
    if(_footView == nil)
    {
        _footView = [[UIView alloc]init];
        if (kDevice_Is_iPhoneX) {
            
            _footView.frame=CGRectMake(0, SCREEN_HEIGHT-45*SCREEN_WIDTH/320-64-34, SCREEN_WIDTH, 45*SCREEN_WIDTH/320);
        }else{
            _footView.frame=CGRectMake(0, SCREEN_HEIGHT-45*SCREEN_WIDTH/320-64, SCREEN_WIDTH, 45*SCREEN_WIDTH/320);
        }
        _footView.backgroundColor = [UIColor whiteColor];
        
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmBtn.frame = CGRectMake(0, 0, kScreenWidth, CGRectGetHeight(_footView.frame));
        confirmBtn.backgroundColor = TBColorProvideBtnbg;
        confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [confirmBtn setTitle:@"返回" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [confirmBtn addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        [_footView addSubview:confirmBtn];
        
    }
    return _footView;
}
- (void)confirmClick:(UIButton*)sender
{
    if(self.navigationController.viewControllers.count > 4)
    {
//        [self.navigationController popViewControllerAnimated:YES];
        
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-4]animated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
- (void)actionTapEmptyDataSetNodataView
{
    [self loadDatas];
}

- (NSMutableArray*)dataArray
{
    if(_dataArray==nil)
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
